package com.company.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.support.json.JSONUtils;
import com.base.common.persistence.Page;
import com.base.common.utils.SQLUtil;
import com.base.common.utils.StringUtils;
import com.base.util.JsonUtil;
import com.company.model.CompanyProject;
import com.company.service.ProjectManageService;
import com.system.service.LoginService;

@Controller
public class ProjectController {

	// 页面actionView的地址
	public static final String toCompanyView = "/company/project";
	public static final String projectList = "/company/project/queryProjectList";
	public static final String projectAdd = "/company/project/add";
	public static final String projectDelete = "/company/project/delete";

	// 页面路径
	public static final String projectPage = "/company/project";
	public static final String projectListPage = "/company/projectList";

	@Autowired
	ProjectManageService projectManageService;

	@RequestMapping(value = toCompanyView, method = RequestMethod.POST)
	public ModelAndView toCompanyView(HttpServletRequest req, HttpServletResponse res) {
		ModelAndView mv = new ModelAndView(projectPage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		List<CompanyProject> list = null;
		try {
			list = projectManageService.findProjectListByParams(companyId, "", "", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("projectList", list);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = projectList, method = RequestMethod.POST)
	public ModelAndView queryProjectList(HttpServletRequest req, HttpServletResponse res, String projectName, String projectNo, String endFlag) {
		// Page<CompanyProject> page = new Page<CompanyProject>();
		ModelAndView mv = new ModelAndView(projectListPage);
		String companyId = req.getSession().getAttribute("companyId").toString();
		List<CompanyProject> list = null;
		try {
			list = projectManageService.findProjectListByParams(companyId, projectName, projectNo, endFlag);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("projectList", list);
		return mv;
	}
	@ResponseBody
	@RequestMapping(value = projectAdd, method = RequestMethod.POST)
	public boolean projectEdit(HttpServletRequest req, HttpServletResponse res,
			Page<CompanyProject> page, CompanyProject companyProject) {
		String companyId = req.getSession().getAttribute("companyId").toString();
		boolean result = false;
		try {
			if ("_empty".equals(companyProject.getId())) {
				companyProject.setId(null);
			}
			companyProject.setCompanyId(companyId);
			result = projectManageService.saveProject(companyProject);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = projectDelete, method = RequestMethod.POST)
	public boolean projectDelete(String ids) {
		boolean result = false;
		try {
			result = projectManageService.deleteProjectByIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
